/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Author: Luke Manrodt
Brief: Register PODs to save to an XML file
Created: 9/9/2011
History:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "Precompiled.hpp"
#include <sstream>

namespace Serializer
{

  static std::vector<ComponentSyncObj>* entitySyncArray;

  void RegisterPod(Handle<Entity> parent, std::string podName, XMLData data)
  {
    if(entitySyncArray == NULL)
    {
      entitySyncArray = new std::vector<ComponentSyncObj>();
    }

    for(unsigned i = 0; i < entitySyncArray->size(); ++i)// each(ComponentSyncObj registeredComp in *entitySyncArray)
    {
      if((*entitySyncArray)[i].entity == parent)
      {
        (*entitySyncArray)[i].podArray.push_back(data);
        return;
      }
    }
    
    ComponentSyncObj newComp;
    newComp.entity = parent;
    newComp.podArray.push_back(data);
    entitySyncArray->push_back(newComp);
  }

  void RegisterInt(Handle<Entity> parent, std::string componentName, std::string podName, int* pod)
  {
    XMLData newPod;
    newPod.componentName = componentName;
    newPod.type = EPT_INT;
    newPod.pod = (void*)pod;

    RegisterPod(parent, podName, newPod);
  }

  void SaveAll(void)
  {
    TiXmlDocument doc;
    doc.LinkEndChild(new TiXmlDeclaration("1.0", "", ""));
    TiXmlElement* compElement = new TiXmlElement("blarg");
    doc.LinkEndChild(compElement);

    for each(ComponentSyncObj currentObj in *entitySyncArray)
    {
      for each(XMLData data in currentObj.podArray)
      {
        std::ostringstream dataStream;
        switch(data.type)
        {
        case EPT_INT: 
          dataStream<<"component=\""<<data.componentName<<"\"  type=\"Int\"  value=\""<<*(int*) (data.pod)<<"\"";
          break;
        case EPT_UINT:
          dataStream<<"component=\""<<data.componentName<<"\"  type=\"Uint\"  value=\""<<*(unsigned*) (data.pod)<<"\"";
          break;
        case EPT_NUMBER:
          dataStream<<"component=\""<<data.componentName<<"\"  type=\"Number\"  value=\""<<*(float*) (data.pod)<<"\"";
          break;
        }
        compElement->LinkEndChild(new TiXmlElement(dataStream.str().c_str()));
      }
    }

    doc.SaveFile("EntityText.xml");
  }

  void SaveEntity(Entity* entity)
  {

  
  }
}